package graph.views.RunningForm;

import graph.views.IToolboxDrawingAction;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

public class ToolboxRunningPanel extends JPanel implements ActionListener {
	private String PLAY = "back";
	private String  BACK = "play";
	private String NEXT = "next";
	private String STOP = "stop";

	private IToolboxDrawingAction toolboxAction;

	public ToolboxRunningPanel() {
		super(new BorderLayout());

		JToolBar toolBar = new JToolBar("Graph Toolbar", 1);
		addButtons(toolBar);
		add(toolBar, BorderLayout.CENTER);

		toolBar.setFloatable(false);

		this.setPreferredSize(new Dimension(60, 00));
		this.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
		//this.setBackground(Color.white);
	}

	public void setToolboxDrawingAction(IToolboxDrawingAction toolboxAction) {
		this.toolboxAction = toolboxAction;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

//		if (command.equals(PLAY)) {
//			toolboxAction.selectAction();
//		} else if (command.equals(BACK)) {
//			toolboxAction.vertexAction();
//		} else if (command.equals(EDGE)) {
//			toolboxAction.edgeAction();
//		} else if (command.equals(DELETE)) {
//			toolboxAction.deleteAction();
//		}
	}

	private void addButtons(JToolBar toolBar) {
		JButton button = null;

		button = makeNavigationButton("back.png", BACK,
				"Back", "Back");

		toolBar.add(button);

		button = makeNavigationButton("play.png", PLAY, "Play",
				"Stop");
		toolBar.add(button);

		button = makeNavigationButton("next.png", NEXT, "Draw a edge", "Edge");
		toolBar.add(button);
		
		 button = makeNavigationButton("stop.png", STOP,
		 "Stop", "stop");
		 toolBar.add(button, BorderLayout.CENTER);
	}

	private JButton makeNavigationButton(String imageName,
			String actionCommand, String toolTipText, String altText) {
		// Look for the image.
		String imgLocation = "../images/" + imageName;
		URL imageURL = this.getClass().getResource(imgLocation);

		// Create and initialize the button.
		JButton button = new JButton();
		button.setActionCommand(actionCommand);
		button.setToolTipText(toolTipText);
		button.addActionListener(this);

		if (imageURL != null) { // image found
			button.setIcon(new ImageIcon(imageURL, altText));
		} else { // no image found
			button.setText(altText);
			System.err.println("Resource not found: " + imgLocation);
		}

		// button.setMargin(new Insets(0, 0, 0, 0));
		// button.setVerticalTextPosition(SwingConstants.BOTTOM);
		// button.setHorizontalTextPosition(SwingConstants.CENTER);

		return button;
	}
}